import { action, observable, toJS } from 'mobx';
import * as API from '../../apis/docWorkflowApi/odocSignGroupApi';

export default class OdocWorkflowSignStore {
	/**
	 * 通用方法
	 * @param map
	 */

	@action
	setProperty = (map) => {
		for (let i in map) {
			this[i] = map[i];
		}
	}

	@observable workflowid = '';// 176
	@observable requestid = '';// 63221
	@observable groupLists = [];
	@observable counts = 20;
	@observable tabid = '';
	@observable pageLoading = false;
	@observable logParams = {};

	@action
	initLogParams = (commonParam) => {
		let { authStr, authSignatureStr, signatureAttributesStr, signatureSecretKey, f_weaver_belongto_userid } = commonParam;
		this.logParams = { authStr, authSignatureStr, signatureAttributesStr, signatureSecretKey, f_weaver_belongto_userid };
	}

	/**
	 * 签字意见分组页面
	 */
	@action('签字意见分组默认加载')
	getSignGroupPageList = () => {
		let params = {
			workflowid: this.workflowid,
			requestid: this.requestid,
			action: 'all',
			counts: this.counts,
			tabid: this.tabid,
			...toJS(this.logParams),
		};
		API.getSignGroupPageData(params).then((data) => {
			if (data.api_status && data.api_status == true) {
				this.groupLists = data.groupLists;
				this.pageLoading = false;
				this.imgForLazyLoad();
			} else {
				this.pageLoading = false;
				this.groupLists = [];
			}
		});
	}

	@action('签字意见分组加载更多')
	signGroupPageLoadMore = (groupid, mouldid, pagenumber, index) => {
		this.pageLoading = true;
		let params = {
			mouldid,
			groupid,
			action: 'more',
			workflowid: this.workflowid,
			requestid: this.requestid,
			pagenumber: parseInt(pagenumber) + 1,
			counts: this.counts,
			tabid: this.tabid,
			...toJS(this.logParams),
		};
		API.getSignGroupPageData(params).then((data) => {
			if (data.api_status && data.api_status == true) {
				let groupdatas = data.groupLists[0].groupDatas;
				this.groupLists[index].groupDatas = [
					...this.groupLists[index].groupDatas,
					...groupdatas,
				];
				this.groupLists[index].pagenumber = parseInt(pagenumber) + 1;
				this.groupLists[index].hasmore = groupdatas.length < this.counts ? '0' : '1';
				this.pageLoading = false;
				this.imgForLazyLoad();
			}
		}).catch(() => {
			this.pageLoading = false;
		});
	}


	@action
	imgForLazyLoad = () => {
		let i = 0;
		let timer = setInterval(() => {
			i++;
			if (window.odocSignImgUtil.odocFormImgLazyLoad && jQuery('.workflow-signSetting')) {
				window.odocSignImgUtil.odocFormImgLazyLoad(jQuery('.workflow-signSetting'));
			}
			if (i >= 4) {
				clearInterval(timer);
			}
		}, 500);
	}
}

